package com.gxa.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.utils.DateUtils;
import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Query;
import com.gxa.modules.app.mapper.CouponStoreMapper;
import com.gxa.modules.app.mapper.PayOrderMapper;
import com.gxa.modules.sys.dto.ComboDto;
import com.gxa.modules.sys.dto.UserDto;
import com.gxa.modules.sys.dto.UserUpdateDto;
import com.gxa.modules.sys.dto.UsersDto;
import com.gxa.modules.sys.entity.Clock;
import com.gxa.modules.sys.entity.Coupon;
import com.gxa.modules.sys.entity.Emp;
import com.gxa.modules.sys.entity.User;
import com.gxa.modules.sys.mapper.CouponMapper;
import com.gxa.modules.sys.mapper.UserMapper;
import com.gxa.modules.sys.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class  UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private CouponStoreMapper couponStoreMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PayOrderMapper payOrderMapper;


    public User queryByName(String name) {
        User user = baseMapper.selectOne(new QueryWrapper<User>().eq("name", name));
        return user;
    }

    @Override
    public PageUtils queryByPage(Map<String, Object> params) {
        String name = (String)params.get("name");
        String id = (String)params.get("id");
        String tel = (String)params.get("tel");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");


        IPage<User> page = this.page(new Query<User>().getPage(params),
                new QueryWrapper<User>().eq(StringUtils.isNotEmpty(name),"name",name)
                        .eq(StringUtils.isNotEmpty(id),"id",id)
                        .eq(StringUtils.isNotEmpty(tel),"tel",tel)
                        .ge(StringUtils.isNotEmpty(startDate),"registration_time",startDate)
                        .le(StringUtils.isNotEmpty(endDate),"registration_time",endDate)
                );
        return new PageUtils(page);
    }

    @Override
    public void add(UserDto userDto) {
        this.baseMapper.add(userDto);
    }

    @Override
    public void update(UserUpdateDto userUpdateDto) {
        this.baseMapper.updateUser(userUpdateDto);
    }

    @Override
    public UsersDto queryAll(Integer userId) {
        UsersDto usersDto = new UsersDto();
        usersDto.setId(userId);
        Date date = new Date(System.currentTimeMillis());
        usersDto.setNowDate(date);
        Integer count = this.couponStoreMapper.queryMyCouponNum(usersDto);
        Integer account = this.userMapper.queryByAccount(usersDto);
        BigDecimal bigDecimal = this.payOrderMapper.queryPrice(usersDto);
        usersDto.setCount(count);
        usersDto.setAccount(account);
        usersDto.setBigDecimal(bigDecimal);
        return usersDto;
    }

}
